package com.zhixin.fans.ks.api;


import com.androidnetworking.common.Priority;
import com.rx2androidnetworking.Rx2ANRequest;
import com.rx2androidnetworking.Rx2AndroidNetworking;
import com.trello.rxlifecycle3.android.ActivityEvent;
import com.zhixin.fans.ks.api.res.ChargeHistory;
import com.zhixin.fans.ks.api.res.CoinHistory;
import com.zhixin.fans.ks.api.res.CoinOption;
import com.zhixin.fans.ks.api.res.FirstChangeInfo;
import com.zhixin.fans.ks.api.res.GoodOption;
import com.zhixin.fans.ks.api.res.IndexModule;
import com.zhixin.fans.ks.api.res.LoginEntity;
import com.zhixin.fans.ks.api.res.MutualContent;
import com.zhixin.fans.ks.api.res.MutualDetail;
import com.zhixin.fans.ks.api.res.MutualTalk;
import com.zhixin.fans.ks.api.res.OrderHistory;
import com.zhixin.fans.ks.api.res.PayInfoRes;
import com.zhixin.fans.ks.api.res.ServiceContact;
import com.zhixin.fans.ks.api.res.SubModule;
import com.zhixin.fans.ks.api.res.UserInfo;
import com.zhixin.fans.ks.api.res.UserInfoDetail;

import org.eenie.common.api.ApiEnv;
import org.eenie.common.api.ThreadTransformer;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.BehaviorSubject;

public class CoupleApi {

    private int PAGE_SIZE = 20;
    private String API_URL = "http://ms2client.ksfsvip.com" + "/" + ApiEnv.BASE_PATH;
    BehaviorSubject<ActivityEvent> mSubject;

    /**
     * QQ登录
     *
     * @param openid
     * @param img_url
     * @param nickname
     * @return
     */
    public Observable<UserInfo> loginByQQ(String openid, String img_url, String nickname) {
        Map<String, Object> params = new HashMap<>();
        params.put("img_url", img_url);
        params.put("openid", openid);
        params.put("nickname", nickname);
        return post("qq-login", params)
                .getObjectObservable(UserInfo.class)
                .compose(new ThreadTransformer<>(mSubject));
    }

    /**
     * 微信登录
     *
     * @param openId
     * @param img_url
     * @param nickName
     * @return
     */
    public Observable<UserInfo> loginByWx(String openId, String img_url, String nickName) {
        Map<String, Object> params = new HashMap<>();
        params.put("code", openId);
        params.put("headimgurl", img_url);
        params.put("nickname", nickName);
        return post("wx-login", params)
                .getObjectObservable(UserInfo.class)
                .compose(new ThreadTransformer<>(mSubject));
    }

    /**
     * 获取用户信息
     *
     * @param uid
     * @return
     */
    public Observable<UserInfoDetail> fetchUserInfo(int uid) {
        Map<String, Object> params = new HashMap<>();
        params.put("uid", uid);
        return post("user-info", params)
                .getObjectObservable(UserInfoDetail.class)
                .compose(new ThreadTransformer<>(mSubject));
    }


    /**
     * 获取充值历史
     *
     * @return
     */
    public Observable<List<ChargeHistory>> fetchChangeHistory() {
        return get("active-list")
                .getObjectListObservable(ChargeHistory.class)
                .compose(new ThreadTransformer<>(mSubject));
    }


    /**
     * 获取首页模块
     *
     * @return
     */
    public Observable<List<IndexModule>> fetchModule() {
        return get("big-type")
                .getObjectListObservable(IndexModule.class)
                .compose(new ThreadTransformer<>(mSubject));
    }


    /**
     * 获取互动列表
     *
     * @param uid
     * @param offset
     * @return
     */
    public Observable<List<MutualContent>> fetchMutual(int uid, int offset) {
        Map<String, Object> params = new HashMap<>();
        params.put("uid", uid);
        params.put("offset", offset);
        params.put("limit", PAGE_SIZE);
        return post("hd-list", params)
                .getObjectListObservable(MutualContent.class)
                .compose(new ThreadTransformer<>(mSubject));
    }

    /**
     * 获取首充信息
     *
     * @return
     */
    public Observable<FirstChangeInfo> fetchFirstChange() {
        return get("sc")
                .getObjectObservable(FirstChangeInfo.class)
                .compose(new ThreadTransformer<>(mSubject));
    }

    /**
     * 获取客服联系方式
     *
     * @return
     */
    public Observable<List<ServiceContact>> fetchContact() {
        return get("kf-qq")
                .getObjectListObservable(ServiceContact.class)
                .compose(new ThreadTransformer<>(mSubject));
    }


    /**
     * 获取首充预支付信息
     *
     * @return
     */
    public Observable<PayInfoRes> fetchFirstChangePay(int uid) {
        Map<String, Object> params = new HashMap<>();
        params.put("uid", uid);
        return post("add-sc", params)
                .getObjectObservable(PayInfoRes.class)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }


    /**
     * 获取互动详情
     *
     * @return
     */
    public Observable<MutualDetail> fetchMutualDetail(int uid, int id) {
        Map<String, Object> params = new HashMap<>();
        params.put("uid", uid);
        params.put("id", id);
        return post("hd-detail", params)
                .getObjectObservable(MutualDetail.class)
                .compose(new ThreadTransformer<>(mSubject));
    }

    /**
     * 发表互动评论
     *
     * @return
     */
    public Observable<List<MutualTalk>> sendMutualTalk(int uid, int id, String content) {
        Map<String, Object> params = new HashMap<>();
        params.put("uid", uid);
        params.put("id", id);
        params.put("content", content);
        return post("hd-dis", params)
                .getObjectListObservable(MutualTalk.class)
                .compose(new ThreadTransformer<>(mSubject));
    }

    /**
     * 获取粉币列表
     *
     * @return
     */
    public Observable<List<CoinOption>> fetchCoinList() {
        return get("fb-list")
                .getObjectListObservable(CoinOption.class)
                .compose(new ThreadTransformer<>(mSubject));
    }

    /**
     * 获取预支付信息
     *
     * @return
     */
    public Observable<PayInfoRes> fetchPayInfo(int uid, int goodId) {
        Map<String, Object> params = new HashMap<>();
        params.put("uid", uid);
        params.put("goods_id", goodId);
        return post("add-money", params)
                .getObjectObservable(PayInfoRes.class)
                .compose(new ThreadTransformer<>(mSubject));
    }

    /**
     * 获取模块内容
     *
     * @param type
     * @return
     */
    public Observable<List<SubModule>> fetchModuleInfo(int type) {
        Map<String, Object> params = new HashMap<>();
        params.put("type", type);
        return post("goods-type", params)
                .getObjectListObservable(SubModule.class)
                .compose(new ThreadTransformer<>(mSubject));
    }

    /**
     * 获取模块商品列表
     *
     * @return
     */
    public Observable<List<GoodOption>> fetchModuleGoods(int classId) {
        Map<String, Object> params = new HashMap<>();
        params.put("class", classId);
        return post("goods-list", params)
                .getObjectListObservable(GoodOption.class)
                .compose(new ThreadTransformer<>(mSubject));
    }

    /**
     * 生成订单
     *
     * @return
     */
    public Observable<String> submitOrder(int uid, String desc, int goods_id) {
        Map<String, Object> params = new HashMap<>();
        params.put("uid", uid);
        params.put("desc", desc);
        params.put("goods_id", goods_id);
        return post("gener-order", params)
                .getObjectObservable(String.class)
                .compose(new ThreadTransformer<>(mSubject));
    }


    /**
     * 获取订单列表
     *
     * @param uid
     * @param offset
     * @return
     */
    public Observable<List<OrderHistory>> fetchOrders(int uid, int offset) {
        Map<String, Object> params = new HashMap<>();
        params.put("uid", uid);
        params.put("offset", offset);
        params.put("limit", PAGE_SIZE);
        return post("order-list", params)
                .getObjectListObservable(OrderHistory.class)
                .compose(new ThreadTransformer<>(mSubject));
    }

    /**
     * 获取充值列表
     *
     * @param uid
     * @param offset
     * @return
     */
    public Observable<List<CoinHistory>> fetchChargeList(int uid, int offset) {
        Map<String, Object> params = new HashMap<>();
        params.put("uid", uid);
        params.put("offset", offset);
        params.put("limit", PAGE_SIZE);
        return post("cz-list", params)
                .getObjectListObservable(CoinHistory.class)
                .compose(new ThreadTransformer<>(mSubject));
    }

    /**
     * 获取用户发布的互动列表
     *
     * @param uid
     * @param offset
     * @return
     */
    public Observable<LoginEntity> fetchUserMutuals(String uid, int offset) {
        Map<String, Object> params = new HashMap<>();
        params.put("uid", uid);
        params.put("offset", offset);
        params.put("limit", PAGE_SIZE);
        return post("my-hd", params)
                .getObjectObservable(LoginEntity.class)
                .compose(new ThreadTransformer<>(mSubject));
    }

    /**
     * 获取用户发布的互动列表
     *
     * @param uid
     * @return
     */
    public Observable<String> sendMutuals(int uid, String imgUrl, String desc) {
        Map<String, Object> params = new HashMap<>();
        params.put("uid", uid);
        params.put("img_url", imgUrl);
        params.put("desc", desc);
        return post("add-hd", params)
                .getObjectObservable(String.class)
                .compose(new ThreadTransformer<>(mSubject));
    }


    /**
     * 上传文件
     *
     * @return
     */
    public Observable<JSONObject> uploadFile(File file) {
        return Rx2AndroidNetworking.upload(API_URL + "/upload-apply")
                .setPriority(Priority.MEDIUM)
                .addMultipartFile("img_url", file)
                .setTag(this)
                .build()
                .getJSONObjectObservable()
                .compose(new ThreadTransformer<>(mSubject));
    }

    private Rx2ANRequest post(String method, Map<String, Object> params) {
        return Rx2AndroidNetworking.post(API_URL + "/{path}")
                .addBodyParameter(params)
                .setPriority(Priority.HIGH)
                .addPathParameter("path", method)
                .build();
    }


    private Rx2ANRequest get(String method) {
        return Rx2AndroidNetworking.get(API_URL + "/{path}")
                .addPathParameter("path", method)
                .setPriority(Priority.HIGH)
                .build();
    }


    private Rx2ANRequest get(String method, Map<String, Object> params) {
        return Rx2AndroidNetworking.get(API_URL + "/{path}")
                .addQueryParameter(params)
                .setPriority(Priority.HIGH)
                .addPathParameter("path", method)
                .build();
    }

    public void bindLife(BehaviorSubject<ActivityEvent> subject) {
        mSubject = subject;
    }


    //    private RequestBody generateFormBody(Map<String, Object> params) {
//        FormBody.Builder builder = new FormBody.Builder();
//        for (String key : params.keySet()) {
//            builder.add(key, String.valueOf(params.get(key)));
//        }
//        return builder.build();
//    }

}
